﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace StoveTop
{
	[Activity (Label = "VocabScreen")]			
	public class VocabScreen : ListActivity
	{
		StoveTop.Models.Vocab[] items;
		List<StoveTop.Models.Vocab> vocab;
		StoveTopDBHelper db;

		protected override void OnCreate(Bundle bundle)
		{
			db = new StoveTopDBHelper (this);
			vocab = db.GetAllVocab ();
			Console.WriteLine (vocab.Count + " vocab words found");
			base.OnCreate(bundle);
			items = vocab.ToArray();
			ListAdapter = new VocabListAdapter(this, items);
		}

		protected override void OnListItemClick(ListView l, View v, int position, long id)
		{
			string tooltip = items [position].GetDescription ();
			Android.Widget.Toast.MakeText(this, tooltip, Android.Widget.ToastLength.Short).Show();
		}
	}

	public class VocabListAdapter : BaseAdapter<string> {
		StoveTop.Models.Vocab[] items;
		Activity context;
		public VocabListAdapter(Activity context, StoveTop.Models.Vocab[] items) : base() {
			this.context = context;
			this.items = items;
		}
		public override long GetItemId(int position)
		{
			return position;
		}
		public override string this[int position] {  
			get { return items[position].GetName(); }
		}
		public override int Count {
			get { return items.Length; }
		}
		public override View GetView(int position, View convertView, ViewGroup parent)
		{
			View view = convertView; // re-use an existing view, if one is available
			if (view == null) // otherwise create a new one
				view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem2, null);
			view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = items[position].GetName();

			//	view.FindViewById<TextView> (Android.Resource.Id.Text2).Text = secText;
			return view;
		}
	}
}